<?php
/*
 * Project:	BanMeOnline.org Author:	Pham Thuc Trieu Created:	Aug, 04 2011
 * Contact:	trieu937@gmail.com
 */

defined('_RIGHT_ACCESS') or die('No direct access');

class component{

	function __construct(){
		$this->webTitle = '';
		$this->webKeyword = '';
		$this->webDescription = '';
		$this->moreScript = '';
		$this->moreCss = '';
		$this->moreMetaTags = '';

		$this->catID = 0;
	}

	function __destruct(){
		unset($this->webTitle);
		unset($this->webKeyword);
		unset($this->webDescription);
		unset($this->moreScript);
		unset($this->moreCss);
		unset($this->moreMetaTags);

		unset($this->catID);
	}

	function showDate($w = 0){

		$html = '';
		switch ($w){
			case 1:
				$html = 'Th&#7913; hai';
			break;
			case 2:
				$html = 'Th&#7913; Ba';
			break;
			case 3:
				$html = 'Th&#7913; T&#432;';
			break;
			case 4:
				$html = 'Th&#7913; N&#259;m';
			break;
			case 5:
				$html = 'Th&#7913; S&#225;u';
			break;
			case 6:
				$html = 'Th&#7913; B&#7843;y';
			break;
			default:
				$html = 'Ch&#7911; nh&#7853;t';
			break;
		}
		
		return $html . ' - ng&#224;y ' . date('d/n/Y') . ' - ' . date('H:m:s');
	}

	function categoryMenu(){
		$Qry = 'SELECT cat_id, cat_name
				FROM categories
				WHERE cat_type = "news" AND cat_active = 1
				ORDER BY cat_order';
		$res = new db_query($Qry);
		return $res;
	}

	function lastUpdate($limit = 6){
		global $lang;
		$html = '';

		$Qry = 'SELECT dat_id, dat_title, dat_summarize, dat_date, dat_images, dat_source, cat_id, cat_name
				FROM datas
				INNER JOIN categories ON (cat_id = dat_category)
				WHERE cat_active = 1 AND dat_active = 1 AND cat_type = "news" AND dat_hide = 0
				ORDER BY dat_date DESC
				LIMIT 0,' . $limit;

		$res = new db_query($Qry);

		if (mysql_num_rows($res->result) > 0){

			while ($row = mysql_fetch_assoc($res->result)){

				$msg_id = $row['dat_id'];

				$html .= '<div id="post-' . $row['dat_id'] . '" class="post">';
				$html .= '<h2 class="title">' . $row['dat_title'] . '</h2>';
				$html .= '<div class="meta">';
				$html .= '<span class="catinfo">' . $lang['Chuyen_muc'] . '<a href="/news/' . $row['cat_id'] . '/' . myKernel::remove_unicode(myKernel::title($row['cat_name'])) . '.html" title="' . myKernel::title($row['cat_name']) . '">' . $row['cat_name'] . '</a></span>';
				$html .= '<span class="post-data">' . $lang['Cap_nhat'] . myKernel::niceTime($row['dat_date']) . '</span>';

				$html .= '</div>';

				$html .= '<div class="entry">';

				$path = 'pictures/small_' . $row['dat_images'];

				if ($row['dat_images'] != '' && @file_exists($path)){
					list($width, $height, $type, $attr) = @getimagesize($path);
					$html .= '<img src="/' . $path . '" width="' . $width . '" height="' . $height . '" class="img-post" title="' . myKernel::title($row['dat_title']) . '" alt="' . myKernel::title($row['dat_title']) . '" />';
				}else
					$html .= '<img src="' . IMG . 'nophoto.gif" width="130" height="130" class="img-post" title="' . myKernel::title($row['dat_title']) . '" alt="' . myKernel::title($row['dat_title']) . '" />';

				$html .= '<h2>' . myKernel::truncate(strip_tags($row['dat_summarize']), 200) . '</h2>';

				$html .= $this->same_news($row['dat_title'], $row['dat_id'], $row['cat_id'], 3);

				$html .= '</div>';

				$html .= '<div class="clear"></div>';

				$html .= '<div><a class="more-link" href="/news-' . $row['dat_id'] . '/' . $row['cat_id'] . '/' . myKernel::remove_unicode(myKernel::title($row['cat_name'])) . '/' . myKernel::remove_unicode(myKernel::title($row['dat_title'])) . '.html" rel="bookmark" title="' . myKernel::title($row['dat_title']) . '">' . $lang['Xem_tiep'] . '</a></div>';

				$html .= '</div>';
				$html .= '<div class="clear"></div>';

			}

			$html .= '<div class="facebook_style" id="facebook_style"><a href="" class="load_more" name="' . CAT_ID . '" id="' . $msg_id . '" title="' . $lang['Xem_them'] . '">' . $lang['Xem_them'] . ' <img src="' . IMG . 'arrow.png" width="11" height="6" alt="' . $lang['Xem_them'] . '" /></a></div>';

		}else
			$html .= '<div class="notice">' . $lang['Noi_dung_dang_cap_nhat'] . '</div>';

		$res->close();
		unset($res);

		return $html;
	}

	function recentEntries($limit = 1, $type = 0){

		$mySql = '';
		$myOrder = 'dat_date DESC';

		if ($type == 1){
			$mySql = ' AND dat_date <= ' . (time() - 43200);
			$myOrder = 'dat_date DESC, dat_hits DESC';
		}

		$Qry = 'SELECT dat_id, dat_title, dat_summarize, dat_images, cat_id, cat_name
				FROM datas
				INNER JOIN categories ON (cat_id = dat_category)
				WHERE cat_active = 1 AND dat_active = 1 AND cat_type = "news" AND dat_hide = 0' . $mySql . '
				ORDER BY ' . $myOrder . '
				LIMIT 0,' . $limit;
		$res = new db_query($Qry);
		return $res;
	}

	function popularPosts($limit = 1){
		$Qry = 'SELECT dat_id, dat_title, dat_summarize, dat_images, cat_id, cat_name
				FROM datas
				INNER JOIN categories ON (cat_id = dat_category)
				WHERE cat_active = 1 AND dat_active = 1 AND cat_type = "news" AND dat_hide = 0
				ORDER BY dat_hits DESC
				LIMIT 0,' . $limit;
		$res = new db_query($Qry);
		return $res;
	}

	function catName($id){

		if (!$id)
			return false;

		global $lang;
		$html = '';

		$Qry = 'SELECT cat_name
				FROM categories
				WHERE cat_id = ' . $id;

		$res = new db_query($Qry);

		if (mysql_num_rows($res->result) > 0){

			$row = mysql_fetch_assoc($res->result);

			$html .= $row['cat_name'];

			$this->webTitle = $lang['Tin_tuc'] . ' ' . $row['cat_name'] . ' - ' . $lang['Domain'];
			$this->webKeyword = $lang['Tin_tuc'] . ',' . $row['cat_name'];
			$this->webDescription = $lang['Tin_tuc'] . ' ' . $row['cat_name'] . ' - ' . $lang['Domain'];

		}

		$res->close();
		unset($res);

		return $html;

	}

	function pageNews($limit = 6){

		global $lang;
		$html = '';
		$msg_id = '';
		$this->catID = CAT_ID;

		$catName = $this->catName(CAT_ID);

		$html .= '<h5 class="title">' . $catName . '</h5>';

		$Qry = 'SELECT dat_id, dat_title, dat_summarize, dat_date, dat_images, dat_source, cat_id, cat_name
				FROM datas
				INNER JOIN categories ON (cat_id = dat_category)
				WHERE cat_active = 1 AND dat_active = 1 AND dat_hide = 0 AND cat_id = ' . CAT_ID . '
				ORDER BY dat_date DESC
				LIMIT 0,' . $limit;

		$res = new db_query($Qry);

		if (mysql_num_rows($res->result) > 0){

			while ($row = mysql_fetch_assoc($res->result)){

				$msg_id = $row['dat_id'];

				$html .= '<div id="post-' . $row['dat_id'] . '" class="post">';
				$html .= '<h2 class="title">' . $row['dat_title'] . '</h2>';
				$html .= '<div class="meta">';
				$html .= '<span class="post-data">' . $lang['Cap_nhat'] . myKernel::niceTime($row['dat_date']) . '</span>';
				$html .= '</div>';

				$html .= '<div class="entry">';

				$path = 'pictures/small_' . $row['dat_images'];

				if ($row['dat_images'] != '' && @file_exists($path)){
					list($width, $height, $type, $attr) = @getimagesize($path);
					$html .= '<img src="/' . $path . '" width="' . $width . '" height="' . $height . '" class="img-post" title="' . myKernel::title($row['dat_title']) . '" alt="' . myKernel::title($row['dat_title']) . '" />';
				}else
					$html .= '<img src="' . IMG . 'nophoto.gif" width="130" height="130" class="img-post" title="' . myKernel::title($row['dat_title']) . '" alt="' . myKernel::title($row['dat_title']) . '" />';

				$html .= '<h2>' . myKernel::truncate(strip_tags($row['dat_summarize']), 200) . '</h2>';

				$html .= $this->same_news($row['dat_title'], $row['dat_id'], $row['cat_id'], 3);

				$html .= '</div>';

				$html .= '<div class="clear"></div>';

				$html .= '<div><a class="more-link" href="/news-' . $row['dat_id'] . '/' . $row['cat_id'] . '/' . myKernel::remove_unicode(myKernel::title($row['cat_name'])) . '/' . myKernel::remove_unicode(myKernel::title($row['dat_title'])) . '.html" rel="bookmark" title="' . myKernel::title($row['dat_title']) . '">' . $lang['Xem_tiep'] . '</a></div>';

				$html .= '</div>';
				$html .= '<div class="clear"></div>';

			}

			$html .= '<div class="facebook_style" id="facebook_style"><a href="" class="load_more" name="' . CAT_ID . '" id="' . $msg_id . '" title="' . $lang['Xem_them'] . '">' . $lang['Xem_them'] . ' <img src="' . IMG . 'arrow.png" width="11" height="6" alt="' . $lang['Xem_them'] . '" /></a></div>';

		}else
			$html .= '<div class="notice">' . $lang['Noi_dung_dang_cap_nhat'] . '</div>';

		$res->close();
		unset($res);

		return $html;
	}

	function same_news($tag = '', $id, $iCat, $limit = 1){
		if ($tag == '')
			return false;

		global $lang;

		$html = '';
		$total_tag = '';
		$mySelect = '';
		$tags = '';
		$myOrder = 'dat_title ASC, dat_date ASC';
		$mySqli = '';

		// Break keyword
		if (substr($tag, strlen($tag) - 1, 1) == ',')
			$tags = substr($tag, 0, strlen($tag) - 1);

		$tags = mb_convert_case($tags, MB_CASE_LOWER, 'UTF-8');

		$arrayTag = explode(',', $tags);

		$total_tag = count($arrayTag);

		foreach ($arrayTag as $value){

			if ($total_tag <= 5 && $value != '&nbsp &nbsp'){

				$mySelect = ', ';
				$mySelect .= "if(dat_title LIKE '{$value}', 50, 1) +
						if(dat_title LIKE '%{$value}%', 50, 2) +
						if(dat_title LIKE '%{$value}', 50, 3) +
						if(dat_title LIKE '{$value}%', 50, 4) +

						if(dat_summarize LIKE '{$value}', 50, 1) +
						if(dat_summarize LIKE '%{$value}%', 50, 2) +
						if(dat_summarize LIKE '%{$value}', 50, 3) +
						if(dat_summarize LIKE '{$value}%', 50, 4) +

						if(dat_detail LIKE '{$value}', 50, 1) +
						if(dat_detail LIKE '%{$value}%', 50, 2) +
						if(dat_detail LIKE '%{$value}', 50, 3) +
						if(dat_detail LIKE '{$value}%', 50, 4)
						+ ";
			}
		}

		if (substr($mySelect, strlen($mySelect) - 2, 2) == '+ ')
			$mySelect = substr($mySelect, 0, strlen($mySelect) - 2);

		if ($mySelect != ''){
			$mySelect .= ' AS nPoint';
			$mySqli = 'HAVING nPoint >= 50';
			$myOrder = 'nPoint DESC, dat_title ASC, dat_date ASC';
		}

		$Qry = 'SELECT cat_id, cat_name, dat_id, dat_title, dat_date, dat_images' . $mySelect . '
				FROM datas
				INNER JOIN categories ON (cat_id = dat_category)
				WHERE cat_type = "news" AND cat_active = 1 AND dat_active = 1 AND dat_hide = 0 AND cat_id = ' . $iCat . ' AND dat_id != ' . $id . '
				' . $mySqli . '
				ORDER BY ' . $myOrder . '
				LIMIT 0,' . $limit;

		$res = new db_query($Qry);

		if (mysql_num_rows($res->result) > 0){

			$html .= '<ul class="items">';

			while ($row = mysql_fetch_assoc($res->result)){

				$html .= '<li class="items"><a href="/news-' . $row['dat_id'] . '/' . $row['cat_id'] . '/' . myKernel::remove_unicode(myKernel::title($row['cat_name'])) . '/' . myKernel::remove_unicode(myKernel::title($row['dat_title'])) . '.html" rel="bookmark" title="' . myKernel::title($row['dat_title']) . '">' . myKernel::truncate($row['dat_title'], 90) . '</a></li>';

			}

			$html .= '</ul>';

		}

		$res->close();
		unset($res);

		return $html;
	}

	function detailExtra(){
		$Qry = 'SELECT dat_id, dat_title, dat_summarize, dat_detail
				FROM datas
				INNER JOIN categories ON (cat_id = dat_category)
				WHERE cat_type = "extra" AND cat_active = 1 AND dat_active = 1 AND dat_id = ' . DAT_ID;
		$res = new db_query($Qry);
		return $res;
	}

	function detailNews(){

		global $lang;
		$html = '';
		$datID = 0;

		if(P_ID > 0){
			$Qry = "SELECT dbr_child_data
					FROM data_break
					WHERE dbr_parent_data = " . DAT_ID ." AND dbr_order = " . P_ID;

			$db_pack = new db_query($Qry);

			if ($pak = mysql_fetch_array($db_pack->result))
				$datID = $pak["dbr_child_data"];

			unset($db_pack);
		}else
			$datID = DAT_ID;

		$Qry = 'SELECT dat_id, dat_title, dat_summarize, dat_date, dat_images, dat_alias, dat_source, cat_id, cat_name, dat_detail, dat_media_url, dat_author
				FROM datas
				INNER JOIN categories ON (cat_id = dat_category)
				WHERE cat_active = 1 AND dat_active = 1 AND dat_id = ' . $datID;

		$res = new db_query($Qry);

		if (mysql_num_rows($res->result) > 0){

			$row = mysql_fetch_assoc($res->result);

			$this->webTitle = $row['dat_title'];
			$this->webKeyword = $row['dat_title'];
			$this->webDescription = str_replace('"', "'", strip_tags($row['dat_summarize']));
			$this->catID = $row['cat_id'];

			$html .= '<div id="post-' . $row['dat_id'] . '" class="post">';
			$html .= '<h2 class="title detail">' . $row['dat_title'] . '</h2>';

			$html .= '<div class="meta">';
			$html .= '<span class="catinfo">' . $lang['Chuyen_muc'] . '<a href="/news/' . $row['cat_id'] . '/' . myKernel::remove_unicode(myKernel::title($row['cat_name'])) . '.html" title="' . myKernel::title($row['cat_name']) . '">' . $row['cat_name'] . '</a></span>';
			$html .= '<span class="post-data">' . $lang['Cap_nhat'] . myKernel::niceTime($row['dat_date']) . '</span>';
			$html .= '<p>';
			$html .= '<a id="favorites" title="' . $lang['Them_vao_yeu_thich'] . '"><img src="' . IMG . 'love-16.png" width="16" height="16" alt="Favorites" />' . $lang['Yeu_thich'] . '</a>';
			$html .= '<a href="/page.php?module=feedback&url=' . urlencode("news-" . $row["dat_id"] . "/" . $row["cat_id"] ."/" . myKernel::remove_unicode($row["cat_name"]) . "/" . myKernel::remove_unicode($row["dat_title"]) . ".html") . '"><img src="' . IMG . 'email-reply.png" width="16" height="16" alt="Feedback" />' . $lang['Phan_hoi'] . '</a>';
			$html .= '<a href="javascript:window.print()"><img src="' . IMG . 'print.gif" width="16" height="16" alt="Print" />' . $lang['In_noi_dung'] . '</a>';
			$html .= '</p>';
			$html .= '</div>';

			$html .= '<div class="entry">';
			$html .= '<h2 class="header">' . strip_tags($row['dat_summarize']) . '</h2>';
			$html .= '<div class="normal">' . myKernel::ADinContents($row['dat_detail']) . '</div>';

			$html .= '<div class="clear"></div>';

			if($row['dat_media_url'] != ''){

				$media = trim($row['dat_media_url']);
		
				$html .= '<div id="media">';
		
				$html .= '<object classid="CLSID:6BF52A52-394A-11D3-B153-00C04F79FAA6" id="Media" width="450" height="350" style="border-style: solid; border-width: 0px"><param name="fullScreen" value="0"><PARAM NAME="autoStart" VALUE="-1"><PARAM NAME="volume" VALUE="100"><param name="AllowChangeDisplaySize" value="true"><param name="AllowScan" value="true"><param name="AnimationAtStart" value="true"><PARAM NAME="stretchToFit" value="true"><PARAM NAME="windowlessVideo" value="true"><param name="ShowDisplay" value="true"><param name="ShowGotoBar" value="true"><param name="ShowStatusBar" value="true"><param name="VideoBorder3D" value="true"><param name="PreviewMode" value="true"><PARAM NAME="enabled" VALUE="-1"><PARAM NAME="enableContextMenu" VALUE="0"><param name="wmode" value="transparent"><param name="rate" value="1"><param name="balance" value="0"><param name="currentPosition" value="0"><param name="defaultFrame" value><param name="playCount" value="10"><param name="currentMarker" value="0"><param name="invokeURLs" value="0"><param name="baseURL" value><param name="mute" value="0"><param name="uiMode" value="full"><param name="SAMIStyle" value><param name="SAMILang" value><param name="SAMIFilename" value><param name="captioningID" value><param name="enableErrorDialogs" value="0"><param name="_cx" value="6450"><param name="_cy" value="6482"><param name="url" value="' . $media . '"><embed type="application/x-mplayer2" pluginspage = "http://www.microsoft.com/windows/mediaplayer/" width="450" height="350" src="' . $media . '" name="player" wmode="transparent" uiMode="full" ShowGotoBar="0" VideoBorder3D="1" TransparentAtStart="1" AnimationAtStart="1" PreviewMode="1" autostart="1" showcontrols="1" enableContextMenu="0" showstatusbar="1" showdisplay="1"></embed></object>';
		
				$html .= '</div>';
				$html .= '<div class="clear"></div>';
		
			}

			$html .= '<div class="source">';
			//Tac gia
			$auThor = 0;
			$auLink = '';
			$athor = new db_query("SELECT au_id,au_title
								   FROM datas,author
								   WHERE au_id = dat_author AND au_active = 1 AND au_id = " . $row["dat_author"] . "
								   ORDER BY rand()
								   LIMIT 0,10");
		
			if(mysql_num_rows($athor->result)>0){
				$au = mysql_fetch_array($athor->result);
				$auThor = $au["au_id"];
				$auLink = '<a href="/author-' . $au["au_id"] . '/' . myKernel::remove_unicode($au["au_title"]) . '.html"><b>' . $au["au_title"] . '</b></a>';
				$html .= $lang['Nguoi_viet'] . $auLink . '<br />';
			}else{
				//But danh
				if($row["dat_alias"] != "")
					$html .= "<b>".$row["dat_alias"]."</b><br />";
			}
			unset($athor);
		
			//Nguon tai lieu
			if($row["dat_source"] != "")
				$html .="<i>" . $lang['Nguon_tin'] . "<b>".$row["dat_source"]."</b></i>";

			$html .= '</div>';

			$html .= '<div class="clear"></div>';

			$html .= $this->dataPart($row['cat_name'], $row['dat_title']);
			$html .= '<div class="clear"></div>';

			$html .= $this->ofAuthor($auThor, $auLink);
			$html .= $this->incatNews($row['dat_id'], $row['cat_id'], 10, 'new');
			$html .= $this->incatNews($row['dat_id'], $row['cat_id'], 10);

			$html .= '</div>';
			$html .= '<div class="clear"></div>';

			$this->updateHits('datas', 'dat_hits', 'dat_id');
		}else{
			$this->webTitle = $lang['Khong_ton_tai_noi_dung_nay'];
			$html .= '<div class="error_warning">' . $lang['Khong_ton_tai_noi_dung_nay'] . '</div>';
		}

		$res->close();
		unset($res);

		return $html;
	}

	function updateHits($table = '', $field = '', $require = ''){

		if ($table == '' or $field == '')
			return false;

		$db_hit = new db_execute('UPDATE ' . $table . ' SET ' . $field . ' = ' . $field . ' + 1 WHERE ' . $require . ' = ' . DAT_ID);
		unset($db_hit);
	}

	function dataPart($catName = '', $title = ''){
		$html = '';
		$current	= 1;
		$totalItem	= 1;

		if(isset($_GET["iPa"]))
			$current = $_GET["iPa"];
		$current	= intval($current);

		if($current < 1)
			$current = 1;
	
		$url = "/news-" . DAT_ID . "/" . CAT_ID . "/" . myKernel::remove_unicode($catName) . "/" . myKernel::remove_unicode($title) . ".html";
	
		$db_count = new db_query("SELECT Count(*) Count
								  FROM data_break
								  WHERE dbr_parent_data = " . DAT_ID);
	
		$myCount = mysql_fetch_array($db_count->result);
		$totalRecord = $myCount['Count'] + 1; //echo $totalRecord;

		$db_count->close();
		unset($db_count);
	
		$html .= '<div class="iPage">';
	
		if($totalRecord > $totalItem){
			$html .= "<center>";
			$html .= showPageBar(2,"<b>Ph&#7847;n</b>","/",$current,$totalItem,$totalRecord,$url,""," <img src=\"/images/b_start.png\" border=\"0\"> "," <img src=\"/images/b_prev.png\" border=\"0\"> "," <img src=\"/images/b_next.png\" border=\"0\"> "," <img src=\"/images/b_end.png\" border=\"0\"> ","classPublic","classChoice","classText","classPrefix");
			$html .= "</center>";
		}

		$html .= '</div>';
		return $html;
	}

	function ofAuthor($id = 0, $auLink = ''){
		global $lang;
		$html = '';

		$Qry = "SELECT cat_name,cat_id,dat_id,dat_title, dat_date
				FROM categories,datas,author
				WHERE cat_id = dat_category AND dat_author = au_id AND dat_hide = 0 AND dat_id <> " . DAT_ID . " AND au_id = " . $id;

		$res = new db_query($Qry);
		if (mysql_num_rows($res->result) > 0){

			$html .= '<div class="otherNews"><h5>' . $lang['Cac_tac_pham_khac'] . $auLink . '</h5><ul>';

			while ($row = mysql_fetch_assoc($res->result)){
				$html .= '<li><a href="/news-' . $row['dat_id'] . '/' . $row['cat_id'] . '/' . myKernel::remove_unicode(myKernel::title($row['cat_name'])) . '/' . myKernel::remove_unicode(myKernel::title($row['dat_title'])) . '.html" rel="bookmark" title="' . myKernel::title($row['dat_title']) . '">' . myKernel::truncate($row['dat_title'], 45) . '</a><span>(' . date('h:s:a - d/m/Y', $row['dat_date']) . ')</span></li>';
			}
			$html .= '</ul></div>';
		}

		$res->close();
		unset($res);

		return $html;
	}

	function incatNews($id, $cat, $limit = 1, $type = ''){

		global $lang;
		$html = '';
		$title = '';
		$mySql = '';

		if ($type == 'new'){
			$mySql = ' AND dat_id != ' . $id . ' AND cat_id = ' . $cat;
			$title = $lang['Tin_moi'];
		}else{
			$mySql = ' AND dat_id < ' . $id . ' AND cat_id = ' . $cat;
			$title = $lang['Cac_tin_khac'];
		}

		$Qry = 'SELECT dat_id, dat_title, dat_date, cat_id, cat_name
				FROM datas
				INNER JOIN categories ON (cat_id = dat_category)
				WHERE cat_active = 1 AND dat_active = 1 AND dat_hide = 0' . $mySql . '
				ORDER BY dat_date DESC
				LIMIT 0,' . $limit;

		$res = new db_query($Qry);
		if (mysql_num_rows($res->result) > 0){

			$html .= '<div class="otherNews"><h5>' . $title . '</h5><ul>';

			while ($row = mysql_fetch_assoc($res->result)){
				$html .= '<li><a href="/news-' . $row['dat_id'] . '/' . $row['cat_id'] . '/' . myKernel::remove_unicode(myKernel::title($row['cat_name'])) . '/' . myKernel::remove_unicode(myKernel::title($row['dat_title'])) . '.html" rel="bookmark" title="' . myKernel::title($row['dat_title']) . '">' . myKernel::truncate($row['dat_title'], 45) . '</a><span>(' . date('h:s:a - d/m/Y', $row['dat_date']) . ')</span></li>';
			}
			$html .= '</ul></div>';
		}

		$res->close();
		unset($res);

		return $html;
	}

	function searchResult($keyword = '', $iCat = 0, $sDate = '', $eDate = ''){
		global $lang;
		$html = '';
		$mySelect = '';
		$mySqli = '';
		$myOrder = 'dat_date DESC, dat_title ASC';

		if (!empty($keyword))
			$this->webTitle = $this->webKeyword = $this->webDescription = $lang['Ket_qua_tim_kiem_voi_tu_khoa'] . '&quot;' . $keyword . '&quot;';
		else
			$this->webTitle = $this->webKeyword = $this->webDescription = $lang['Ket_qua_tim_kiem'];

		if ($iCat > 0){
			$mySqli .= " AND cat_id = " . $iCat;
			$myOrder = " ORDER BY dat_date DESC";
		}

		if ($sDate && $eDate){
			$mySqli .= " AND dat_date >= " . $sDate . " AND dat_date <= " . $eDate;
		}

		if (!empty($keyword)){
			$mySelect .= ", if(dat_title LIKE '{$keyword}', 50, 1) +
						if(dat_title LIKE '%{$keyword}%', 50, 2) +
						if(dat_title LIKE '%{$keyword}', 50, 3) +
						if(dat_title LIKE '{$keyword}%', 50, 4) +

						if(dat_summarize LIKE '{$keyword}', 50, 1) +
						if(dat_summarize LIKE '%{$keyword}%', 50, 2) +
						if(dat_summarize LIKE '%{$keyword}', 50, 3) +
						if(dat_summarize LIKE '{$keyword}%', 50, 4) +

						if(dat_detail LIKE '{$keyword}', 50, 1) +
						if(dat_detail LIKE '%{$keyword}%', 50, 2) +
						if(dat_detail LIKE '%{$keyword}', 50, 3) +
						if(dat_detail LIKE '{$keyword}%', 50, 4)";

			if (! empty($mySelect)){
				$mySelect .= ' AS nPoint';
				$mySqli = 'HAVING nPoint >= 50';
				$myOrder = 'nPoint DESC, dat_title ASC, dat_date ASC';
			}
		}

		$Qry = 'SELECT dat_id, dat_title, dat_date, dat_images, cat_id, cat_name, dat_source, dat_summarize' . $mySelect . '
				FROM datas
				INNER JOIN categories ON (cat_id = dat_category)
				WHERE cat_type = "news" AND cat_active = 1 AND dat_active = 1
				' . $mySqli . '
				ORDER BY ' . $myOrder . '
				LIMIT 0, 30';
//echo $Qry;
		$res = new db_query($Qry);

		if (mysql_num_rows($res->result) > 0){

			while ($row = mysql_fetch_assoc($res->result)){

				$msg_id = $row['dat_id'];

				$html .= '<div id="post-' . $row['dat_id'] . '" class="post">';
				$html .= '<h2 class="title">' . $row['dat_title'] . '</h2>';
				$html .= '<div class="meta">';
				$html .= '<span class="post-data">' . $lang['Cap_nhat'] . myKernel::niceTime($row['dat_date']) . '</span>';
				$html .= '</div>';

				$html .= '<div class="entry">';

				$path = 'pictures/small_' . $row['dat_images'];

				if ($row['dat_images'] != '' && @file_exists($path)){
					list($width, $height, $type, $attr) = @getimagesize($path);
					$html .= '<img src="/' . $path . '" width="' . $width . '" height="' . $height . '" class="img-post" title="' . myKernel::title($row['dat_title']) . '" alt="' . myKernel::title($row['dat_title']) . '" />';
				}else
					$html .= '<img src="' . IMG . 'nophoto.gif" width="130" height="130" class="img-post" title="' . myKernel::title($row['dat_title']) . '" alt="' . myKernel::title($row['dat_title']) . '" />';

				$html .= '<h2>' . myKernel::truncate(strip_tags($row['dat_summarize']), 200) . '</h2>';

//				$html .= $this->same_news($row['dat_title'], $row['dat_id'], $row['cat_id'], 3);

				$html .= '</div>';

				$html .= '<div class="clear"></div>';

				$html .= '<div><a class="more-link" href="/news-' . $row['dat_id'] . '/' . $row['cat_id'] . '/' . myKernel::remove_unicode(myKernel::title($row['cat_name'])) . '/' . myKernel::remove_unicode(myKernel::title($row['dat_title'])) . '.html" rel="bookmark" title="' . myKernel::title($row['dat_title']) . '">' . $lang['Xem_tiep'] . '</a></div>';

				$html .= '</div>';
				$html .= '<div class="clear"></div>';

			}
		}else
			$html .= '<div class="notice">' . $lang['Khong_co_ket_qua'] . '</div>';

		return $html;
	}

	function listAuthor(){
		global $lang;
		$this->webTitle = $this->webKeyword = $this->webDescription = $lang['Tac_gia'];

		$Qry = 'SELECT au_id,au_title,au_summarize,au_images
				FROM author
				WHERE au_active = 1';
		$res = new db_query($Qry);

		return $res;
	}

	function detailAuthor($id){
		$Qry = 'SELECT *
				FROM author
				WHERE au_active = 1 AND au_id = ' . $id;
		$res = new db_query($Qry);

		return $res;
	}

	function ads($pos = ''){

		$mysql = '';
		if ($pos == 'top')
			$mysql = ' LIMIT 0,1';

		$Qry = 'SELECT ban_id, ban_title, ban_width, ban_height, ban_images, ban_target
				FROM banners
				WHERE ban_position = "' . $pos . '" AND ban_active = 1
				ORDER BY ban_order' . $mysql;
		$res = new db_query($Qry);
		return $res;
	}

	function footer($id){

		$html = '';

		$Qry = "SELECT dat_detail
				FROM datas
				WHERE dat_active = 1 AND dat_id = " . $id;

		$res = new db_query($Qry);

		if(mysql_num_rows($res->result)>0){
			$row = mysql_fetch_array($res->result);
			$html .= trim($row["dat_detail"]);
		}

		return $html;
	}

	function plugin($id){

		$html = '';

		$Qry = "SELECT dat_title, dat_detail
				FROM datas
				WHERE dat_active = 1 AND dat_id = " . $id;

		$res = new db_query($Qry);

		return $res;
	}

	function hitCounter(){
		global $lang;
		$html = array();

		//Get user online
		$uAct = 0;
		$db_count = new db_query("SELECT *
								  FROM active_users");
		$uAct = number_format(mysql_num_rows($db_count->result),0,'','.');
		unset($db_count);

		//Select all count with id = 0
		$visited = 0;
		$db_visited = new db_query("SELECT vi_count
									FROM visited
									WHERE vi_id = 0");
		if($vis = mysql_fetch_array($db_visited->result)){
			$visited = number_format($vis["vi_count"],0,'','.');
		}
		unset($db_visited);

		$html = array(
				$lang['Truy_cap']=>$visited,
				$lang['Truc_tuyen']=>$uAct);

		return $html;
	}

	function gallery(){
		global $lang;
		$html = '';

		$Qry = 'SELECT gal_id, gal_title, gal_link, gal_target, gal_images
				FROM gallery
				WHERE gal_parent_id = 0 AND gal_active = 1
				ORDER BY gal_order ASC';

		$res = new db_query($Qry);

		if (mysql_num_rows($res->result)){

			$html .= '<div class="gallery">';
			$html .= '<div id="slideshow" class="slideshow">';

			while ($row = mysql_fetch_array($res->result)) {

				$html .= '<div id="fade_' . $row['gal_id'] . '" class="pics">';

				$path = 'gallery/thumb_' . $row['gal_images'];

				if($row['gal_images'] != '' && @file_exists($path)){
					list($width, $height, $type, $attr) = @getimagesize($path);
					$html .= '<a href="/gallery-' . $row['gal_id'] . '/' . myKernel::remove_unicode(myKernel::title($row['gal_title'])) . '.html" target="' . $row['gal_target'] . '" id="gallery-' . $row['gal_id'] . '" title="' . $row['gal_title'] . '"><img src="/' . $path . '" width="' . $width . '" height="' . $height . '" class="the_image" alt="' . $row['gal_title'] . '" title="' . $row['gal_title'] . '" /></a>';
				}else
					$html .= '<a href="/gallery-' . $row['gal_id'] . '/' . myKernel::remove_unicode(myKernel::title($row['gal_title'])) . '.html" target="' . $row['gal_target'] . '" id="gallery-' . $row['gal_id'] . '" title="' . $row['gal_title'] . '"><img src="' . IMG . 'gallery.jpg" width="270" height="270" class="the_image" alt="" title="" /></a>';

				// Child Gallery
				$Sql = 'SELECT gal_id, gal_title, gal_images
						FROM gallery
						WHERE gal_parent_id = ' . $row['gal_id'] . '
						ORDER BY gal_order ASC';

				$que = new db_query($Sql);

				while ($gal = mysql_fetch_array($que->result)) {

					$pathC = 'gallery/thumb_' . $gal['gal_images'];

					if($gal['gal_images'] != '' && @file_exists($pathC)){
						list($width, $height, $type, $attr) = @getimagesize($pathC);
						$html .= '<a href="/gallery-' . $row['gal_id'] . '/' . myKernel::remove_unicode(myKernel::title($row['gal_title'])) . '.html" target="' . $row['gal_target'] . '" id="image-' . $gal['gal_id'] . '" title="' . $gal['gal_title'] . '"><img src="/' . $pathC . '" width="' . $width . '" height="' . $height . '" class="the_image" alt="' . $gal['gal_title'] . '" title="' . $gal['gal_title'] . '" /></a>';
					}

				}

				$html .= '</div>';

				$html .= '<pre><code class="mix">$(\'#fade_' . $row['gal_id'] . '\').cycle();</code></pre>';

				$que->close();
				unset($que);
			}

			$html .= '<div class="clear"></div>';
			$html .= '</div>';
			$html .= '<div class="clear"></div>';

			mysql_data_seek($res->result,0);

			$html .= '<ul id="click">';

			$i = 0;
			while ($row = mysql_fetch_array($res->result)) {
				$i++;

				if ($i == mysql_num_rows($res->result))
					$html .= '<li class="last"><a href="' . $row['gal_id'] . '" class="click" id="click_' . $row['gal_id'] . '" title="' . $row['gal_title'] . '">' . $row['gal_title'] . '</a></li>';
				else
					$html .= '<li><a href="' . $row['gal_id'] . '" class="click" id="click_' . $row['gal_id'] . '" title="' . $row['gal_title'] . '">' . $row['gal_title'] . '</a></li>';

			}
			$html .= '</ul>';
			$html .= '<div class="clear"></div>';
			$html .= '</div>';
		}

		$res->close();
		unset($res);

		return $html;
	}

	function detailGallery(){
		$Qry = 'SELECT gal_id, gal_title, gal_teaser, gal_images
				FROM gallery
				WHERE gal_active = 1 AND gal_id = ' . DAT_ID;

		$res = new db_query($Qry);
		return $res;
	}

	function galleryImg(){
		$Qry = 'SELECT gal_id, gal_title, gal_images
				FROM gallery
				WHERE gal_active = 1 AND gal_parent_id = ' . DAT_ID;

		$res = new db_query($Qry);
		return $res;
	}
}